﻿using Microsoft.EntityFrameworkCore;
namespace CoreModule.Storages
{
    /// <summary>
    /// SQLite 数据库，默认
    /// </summary>
    public class MySqlDbStorage : DbStorage
    { /// <summary>
      /// 
      /// </summary>
      /// <param name="ipAddress"></param>
      /// <param name="userName"></param>
      /// <param name="userPassword"></param>
        public static void UpdateConnectionString(string ipAddress, string userName, string userPassword, string database)
        {
            ConnectionString = string.Format("Server={0};Port=3306;Database={3}; User={1};Password={2};SslMode=none;AllowPublicKeyRetrieval=true;", ipAddress, userName, userPassword, database);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static DateTime GetServerTime()
        {
            DateTime dateTime = DateTime.Now;

            try
            {
                CoreModule.Storages.MySqlDbStorage dbStorage = new CoreModule.Storages.MySqlDbStorage();
                dateTime = dbStorage.MeasurementArgs.FromSql($"SELECT  `Id`,`VaporInterval`,`VaporLowValue`,`VaporHighValue`,`ClientId`, NOW()  AS RefTime FROM MeasurementArgs LIMIT 1").First().RefTime;
                return dateTime;
            }
            catch (Exception ex)
            {
                return DateTime.Now;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        public static string ConnectionString = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionString = ConnectionString;
            optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
        }
    }
}
